package com.jz.community.moduleshopping.integralGoods.ui.view;

import android.app.ActionBar;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.integralGoods.bean.IntegralCouponInfo;

import java.util.List;


/**
 * Created by Tony on 2018/7/03.
 * <p>
 */
public class IntegralCouponExchangePop extends PopupWindow implements View.OnClickListener {
    private Context mContext;
    private RelativeLayout layout;
    private TextView integral_money_tv, integral_available_money_tv, integral_tv, integral_coupon_goods_tv, integral_coupon_shop_tv, integral_coupon_time_tv;
    private SuperButton integral_btn;
    private ImageView integral_close_tv;
    private IntegralCouponListener integralCouponListener;

    public void setIntegralCouponListener(IntegralCouponListener integralCouponListener) {
        this.integralCouponListener = integralCouponListener;
    }

    public IntegralCouponExchangePop(Context context, RelativeLayout layout) {
        this.mContext = context;
        this.layout = layout;
        handleShow();
    }

    public void handleShowData(List<IntegralCouponInfo.EmbeddedBean.CouponInfoesBean> couponInfoes, int position) {
        integral_money_tv.setText(AppConstants.MONEY + couponInfoes.get(position).getCouponAmount());
        integral_available_money_tv.setText(couponInfoes.get(position).getCouponName());
        integral_tv.setText(couponInfoes.get(position).getMinIntegral() + "");
        integral_coupon_time_tv.setText("指定时间:" + couponInfoes.get(position).getUseEndDate());
        if (!Preconditions.isNullOrEmpty(couponInfoes.get(position).getShopName())){
            SHelper.vis(integral_coupon_shop_tv);
            integral_coupon_shop_tv.setText("指定商家:" + couponInfoes.get(position).getShopName());
        }else {
            SHelper.invis(integral_coupon_shop_tv);
        }

        if (!Preconditions.isNullOrEmpty(couponInfoes.get(0).getCouponItemList())){
            integral_coupon_goods_tv.setText("指定商品:" + couponInfoes.get(0).getCouponItemList().get(0).getGoodsName());
            SHelper.vis(integral_coupon_goods_tv);
        }else {
            SHelper.invis(integral_coupon_goods_tv);
        }
    }

    private RelativeLayout view;

    private void handleShow() {
        view = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.module_shopping_integral_exchange_item_layout, null);
        this.setContentView(view);
        this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        this.setHeight(ActionBar.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        showAtLocation(layout, Gravity.CENTER, 0, 0);
        initView(view);
    }

    private void initView(View view) {
        integral_money_tv = view.findViewById(R.id.integral_money_tv);
        integral_available_money_tv = view.findViewById(R.id.integral_available_money_tv);
        integral_tv = view.findViewById(R.id.integral_tv);
        integral_coupon_time_tv = view.findViewById(R.id.integral_coupon_time_tv);
        integral_coupon_shop_tv = view.findViewById(R.id.integral_coupon_shop_tv);
        integral_coupon_goods_tv = view.findViewById(R.id.integral_coupon_goods_tv);
        integral_btn = view.findViewById(R.id.integral_btn);
        integral_close_tv = view.findViewById(R.id.integral_close_tv);
        integral_btn.setOnClickListener(this);
        integral_close_tv.setOnClickListener(this);
        view.setOnClickListener(v -> {
            hideDissmiss();
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.integral_btn) {
            integralCouponListener.integralCouponResult();
            hideDissmiss();
        }
        if (v.getId() == R.id.integral_close_tv) {
            hideDissmiss();
        }
    }

    /**
     * 移除dialog
     */
    public void hideDissmiss() {
        dismiss();
    }

    public void show() {
        handleShow();
    }


    public interface IntegralCouponListener {
        void integralCouponResult();
    }
}
